﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITSP.SharePoint.CacheRefresh.Common;
using ITSP.SharePoint.CacheRefresh.Entities;
using ITSP.SharePoint.CacheRefresh.Interfaces;
using Microsoft.SharePoint;

namespace ITSP.SharePoint.CacheRefresh.Adapters
{
    /// <summary>
    /// Convert a SharePoint List Item to a SharePoint Based Cache Configuration Item
    /// </summary>
    public static class SPListItemToSPCacheConfigItem
    {
        public static SPCacheConfigItem Convert(SPListItem item)
        {
            SPCacheConfigItem cacheItem = new SPCacheConfigItem();
            cacheItem.ListItemId = item.ID;
            cacheItem.CacheKey = item.Title;
            cacheItem.Description = item[Constants.Fields.CacheDescription].ToString();
            cacheItem.CacheDependencyPath = item[Constants.Fields.CacheFileDependencyPath].ToString();
            cacheItem.CacheFrequency = Int32.Parse(item[Constants.Fields.CacheFrequency].ToString());
            cacheItem.CacheEnabled = item[Constants.Fields.CacheEnabled].ToString().ToLower() == "true";

            return cacheItem;
        }
    }

    /// <summary>
    /// Convert a SharePoint List Item to a Cache Configuration Item
    /// </summary>
    public class SPListItemToICacheConfigItemAdapter : ICacheConfigItem
    {
        public SPListItemToICacheConfigItemAdapter(SPListItem item)
        {
            this.ListItemId = item.ID;
            this.CacheKey = item.Title;
            this.Description = item[Constants.Fields.CacheDescription].ToString();
            this.CacheDependencyPath = item[Constants.Fields.CacheFileDependencyPath].ToString();
            this.CacheFrequency = Int32.Parse(item[Constants.Fields.CacheFrequency].ToString());
            this.CacheEnabled = item[Constants.Fields.CacheEnabled].ToString().ToLower() == "true";
        }

        public int ListItemId { get; set; }

        public string CacheKey { get; set; }

        public string Description { get; set; }
       
        public string CacheDependencyPath
        {
            get
            {
                // TODO: Implement this property getter
                throw new NotImplementedException();
            }
            set
            {
                // TODO: Implement this property setter
                throw new NotImplementedException();
            }
        }

        public int CacheFrequency { get; set; }

        public bool CacheEnabled { get; set; }

        public bool Exists { get; set; }

        public static ICacheConfigItem Convert(SPListItem item)
        {
            SPListItemToICacheConfigItemAdapter adapter = new SPListItemToICacheConfigItemAdapter(item);
            ICacheConfigItem cacheItem = adapter;

            return cacheItem;
        }
    }
}